<?php
class sales_finder_sales{
    var $column_originalbn = '发货单号';
    function column_originalbn($row){
      
      $iostock = &app::get('ome')->model('iostock');
      $original = $iostock->getList('original_bn',array('iostock_bn'=>$row['iostock_bn']));
      return $original[0]['original_bn'];
    }
    var $column_shopbn = '店铺编号';
    function column_shopbn($row){
      $iostock = &app::get('ome')->model('shop');
      $original = $iostock->getList('shop_bn',array('shop_id'=>$row['shop_id']));
     
      return $original[0]['shop_bn'];
    }
    var $column_branchbn = '仓库编号';
    function column_branchbn($row){
      $sql = 'SELECT branch_bn FROM sdb_ome_branch WHERE branch_id like \''.$row['branch_id'].'\'';
      $rows = kernel::database()->select($sql);
      $ori_bn = $rows[0]['branch_bn'];
      return $ori_bn;
    }
    var $column_detail = 'detail';
    function detail_edit($id){        
        $render = app::get('sales')->render();
        $oItem = kernel::single("ome_mdl_sales_items");
        $items = $oItem->getList('*',array('sale_id'=>$id));
        $sales = kernel::single('ome_mdl_sales')->getList('*',array('sale_id'=>$id));
        $products = &app::get('ome')->model('products');
        $sql = "select branch_bn from sdb_ome_branch where branch_id='".$sales[0]['branch_id']."'";
        $branch_bn = kernel::database()->select($sql);
        $sql2 = "select name from sdb_ome_branch where branch_id='".$sales[0]['branch_id']."'";
        $branch_name = kernel::database()->select($sql2);
        $sql3 = "select name from sdb_ome_shop where shop_id='".$sales[0]['shop_id']."'";
        $shopname = kernel::database()->select($sql3);
        $sql4 = "select uname from sdb_ome_members where member_id='".$sales[0]['member_id']."'";
        $uname = kernel::database()->select($sql4);
        $nums = $oItem->getList('nums',array('sale_id'=>$id));
        foreach($nums as $k=>$v){
            $num +=$v['nums'];
        }
        if($sales[0]['pay_status'] == '0'){
           $data['pay_status'] = "未支付";
        }else{
           $data['pay_status'] = "已支付";
        }
        foreach($items as $k=>$v){
            $name = $products->getList('name',array('bn'=>$v['bn']));
            $items[$k]['pname'] = $name[0]['name'];
        }
        $iostock = &app::get('ome')->model('iostock');
        $order_items = &app::get('ome')->model('order_items');
        $salesbn = kernel::single('ome_mdl_sales')->getList('iostock_bn',array('sale_id'=>$id));
        
		$iostock_instance = kernel::service('ome.iostock');        
        eval('$iostock_type='.get_class($iostock_instance).'::DIRECT_LIBRARAY;');
        
		//正销售单 跟负销售单 分开处理
        if($num > 0){
            $original = $iostock->getList('original_bn,type_id',array('iostock_bn'=>$salesbn[0]['iostock_bn']));//获取发货单
            $item_id = $iostock->getList('original_item_id',array('iostock_bn'=>$salesbn[0]['iostock_bn']));
           
            if($original[0]['type_id'] == $iostock_type){
            	$objIsoItems = &app::get('taoguaniostockorder')->model('iso_items');
             	foreach($item_id as $k=>$v){
	               $item_detail = $objIsoItems->getList('bn,nums,price,product_name',array('iso_items_id'=>$v['original_item_id']));
	               $item_detail[0]['pname'] = $item_detail[0]['product_name'];
	               $item_detail[0]['number'] = $item_detail[0]['nums'];
	               $item_detail[0]['amount'] = $item_detail[0]['nums'] * $item_detail[0]['price'];
	               
	               $dataitems[] = $item_detail[0];
	            }
            }else{
            	$delivery_item = &app::get('ome')->model('delivery_items_detail');
             	$orders = &app::get('ome')->model('orders');
	            foreach($item_id as $k=>$v){
	               $item_detail = $delivery_item->getList('order_id,bn,number,price,amount',array('item_detail_id'=>$v['original_item_id']));
	               $orderbn = $orders->getList('order_bn',array('order_id'=>$item_detail[0]['order_id']));
	               $pname = $products->getList('name',array('bn'=>$item_detail[0]['bn']));
	               $item_detail[0]['order_bn'] = $orderbn[0]['order_bn'];
	               $item_detail[0]['pname'] = $pname[0]['name'];
	               $dataitems[] = $item_detail[0];
	            }
            }
            
           
        }else{
            $original = $iostock->getList('original_bn',array('iostock_bn'=>$salesbn[0]['iostock_bn']));//获取销售单
            $o_sale_id = kernel::single('ome_mdl_sales')->getList('sale_id',array('sale_bn'=>$original[0]['original_bn']));//获取销售单sale_id
            $o_iostock_id = kernel::single('ome_mdl_sales_items')->getList('iostock_id',array('sale_id'=>$o_sale_id[0]['sale_id']));//获取出入库单的iostock_id
            foreach($o_iostock_id as $k_m => $v_m){
                $item_id_tmp = $iostock->getList('original_item_id',array('iostock_id'=>$v_m['iostock_id']));
                $item_id[] = $item_id_tmp[0];
            }
            $delivery_item = &app::get('ome')->model('delivery_items_detail');
            $orders = &app::get('ome')->model('orders');
            foreach($item_id as $k=>$v){
               $item_detail = $delivery_item->getList('order_id,bn,number,price,amount',array('item_detail_id'=>$v['original_item_id']));
               foreach($items as $k_i=>$v_i){
                   foreach($item_detail as $k_o=>$v_o){
                        if($v_i['bn'] == $v_o['bn']){
                           $orderbn = $orders->getList('order_bn',array('order_id'=>$item_detail[0]['order_id']));
                           $pname = $products->getList('name',array('bn'=>$item_detail[0]['bn']));
                           $item_detail[0]['order_bn'] = $orderbn[0]['order_bn'];
                           $item_detail[0]['pname'] = $pname[0]['name'];
                           $item_detail[0]['number'] = $v_i['nums'];
                           $item_detail[0]['price'] = -$v_i['price'];
                           $item_detail[0]['amount'] = -($v_i['nums'] * $v_i['price']);
                           $dataitems[] = $item_detail[0];
                        }
                   }
               }
            }
        }
        $sales[0]['ori_bn'] = $original[0]['original_bn'];
        $sales[0]['time'] = date("Y-m-d H:i:s",$sales[0]['sale_time']);
        $data['branch_bn'] = $branch_bn[0]['branch_bn'];
        $data['name']      = $branch_name[0]['name'];
        $data['shopname']  = $shopname[0]['name'];
        $data['uname']     = $uname[0]['uname'];
        $data['num']       = $num;
        $render->pagedata['data'] = $data;
        $render->pagedata['dataitems'] = $dataitems;
        $render->pagedata['sales'] = $sales[0];
        $render->display('detail.html');

    }


}
